package com.projectsquirrel;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class NewObservation3 extends Activity {
	private static final int PREFERENCES_GROUP_ID = 11;
	private static final int PAGE_2 = 12;
	private static final int PAGE_3 = 13;
	private static final int PAGE_4 = 14;
	private static final int PAGE_5 = 15;
	private static final int PAGE_6 = 16;
	private static final int PAGE_7 = 17;
	
	private LocationManager myManager;
	private LocListener Location_Listener;

	//private boolean onEmulator;

	private static Observation newObs;
		
	
    static final int TIME_DIALOG_ID = 999990099;
    static final int DATE_DIALOG_ID = 999990098;

    private Button nextObsButton1p5;
    private EditText generalComments;
    private SharedPreferences pref;
    private Context context;
    private ToggleButton SubmittedBeforeButton;
    TextView submittedBeforeText;
    private EditText numofGraySquirrelsEditText;
    private EditText numofFoxSquirrelsEditText;
    private TextView completitionStatus ;
    private ToggleButton newLocationButton;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.observationformbd);
	    final String TAG = "Project_2_Group_6";
	    ImageButton infoIconButton = (ImageButton) findViewById(R.id.infoScreenIcon);
	    ImageButton settingsIconButton = (ImageButton) findViewById(R.id.settingsScreenIcon);
	    //onEmulator = getIntent().getExtras().getBoolean("OnEmulator");
	    nextObsButton1p5 = (Button) findViewById(R.id.nextObsScreen1p5);	    
	    SubmittedBeforeButton = (ToggleButton) findViewById(R.id.sumbBeforeButton);
	    submittedBeforeText = (TextView) findViewById(R.id.textView1ac);
	    completitionStatus = (TextView) findViewById(R.id.textView2);
	    numofGraySquirrelsEditText = (EditText) findViewById(R.id.numOfGrayEditText);
	    numofFoxSquirrelsEditText= (EditText) findViewById(R.id.numOfFoxEditText);
	    completitionStatus.setText("32% complete");
	    generalComments = (EditText) findViewById(R.id.generalCommentText);
	    	    
	     newLocationButton = (ToggleButton) findViewById(R.id.newLocButton);
	     SubmittedBeforeButton = (ToggleButton) findViewById(R.id.sumbBeforeButton);

	    context = getApplicationContext();
	    pref = getSharedPreferences("AppSettings",MODE_PRIVATE);
	  	    
	    
	    //setup the gps manager
	    myManager = (LocationManager) getSystemService(LOCATION_SERVICE);	
	    //Location_Listener = new LocListener(myManager,this);
	    Location_Listener = LocListener.getInstance(myManager, this);

	    String email = pref.getString("userEmail", null);
	    
	    newObs = Observation.getInstance(email);
	    
	     numofGraySquirrelsEditText.setText(newObs.getNumOfGraySquirrels());
	     numofFoxSquirrelsEditText.setText(newObs.getNumOfFoxSquirrels());
	     generalComments.setText(newObs.getGeneralComments());
	    //newObs = new Observation(pref.getString("userEmail", null));
	    /*
		 * Please Note!
		 * This question only needs to be asked once and it will
		 * be entered into user Preferences, it is asking if 
		 * a user has submitted data for a different
		 * site before. If a user answers yes, this will
		 * become an auto-complete answer, if a user enters
		 * "NO" then it will re appear until a counter for the
		 * number times a user submits an obs
		 * while selecting "NO" on using them same site
		 * if the counter >= 2 this question will disappear.
		 * 
		 *   -Daniel
		 */
	    if(pref.getBoolean("SubmitDifSiteBefore", false)==true){
	    	SubmittedBeforeButton.setChecked(true);
	    	SubmittedBeforeButton.setVisibility(View.GONE);
	    	submittedBeforeText.setVisibility(View.GONE);
		}
	    
	    
	
		numofGraySquirrelsEditText.addTextChangedListener(new TextWatcher(){
			public void afterTextChanged(Editable s) {
				if (numofGraySquirrelsEditText.getText() == null || numofGraySquirrelsEditText.getText().length() == 0)
					Toast.makeText(getApplicationContext(),"Please enter a value for Gray Squirrels",1000).show();
				else
					newObs.setNumOfGray(numofGraySquirrelsEditText.getText().toString());
	    
		}
		 
		    public void beforeTextChanged(CharSequence s, int start, int count,
		            int after) {
		        // TODO Auto-generated method stub
		 
		    }
		 
		    public void onTextChanged(CharSequence s, int start, int before,
		            int count) {
		        // TODO Auto-generated method stub
		 
		    }
		});
		numofFoxSquirrelsEditText.addTextChangedListener(new TextWatcher(){
			public void afterTextChanged(Editable s) {
				if (numofFoxSquirrelsEditText.getText() == null || numofFoxSquirrelsEditText.getText().length() == 0)
					Toast.makeText(getApplicationContext(),"Please enter a value for Fox Squirrels",1000).show();
				else
					newObs.setNumOfGray(numofGraySquirrelsEditText.getText().toString());
	    
		}
		 
		    public void beforeTextChanged(CharSequence s, int start, int count,
		            int after) {
		        // TODO Auto-generated method stub
		 
		    }
		 
		    public void onTextChanged(CharSequence s, int start, int before,
		            int count) {
		        // TODO Auto-generated method stub
		 
		    }
		});
		
	    infoIconButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(NewObservation3.this, infoMenu.class);
		        startActivity(i);
			}	    	
	    });
	    
	    settingsIconButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(NewObservation3.this, settingsMenu.class);
		        startActivity(i);
			}	    	

	    });

	    

	    nextObsButton1p5.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
						
				
				if(newLocationButton.isChecked()){
					newObs.setUsingSameSite("YES");
				}
				else{
					newObs.setUsingSameSite("NO");
				}
				/*
				 * Please Note!
				 * This question only needs to be asked once and it will
				 * be entered into user Preferences, it is asking if 
				 * a user has submitted data for a different
				 * site before. If a user answers yes, this will
				 * become an auto-complete answer, if a user enters
				 * "NO" then it will re appear until a counter for the
				 * number times a user submits an obs
				 * while selecting "NO" on using them same site
				 * if the counter >= 2 this question will disappear.
				 * 
				 *   -Daniel
				 */
				
				if(SubmittedBeforeButton.isChecked()){
					newObs.setUsingDifSite("YES");
				}
				else{
					newObs.setUsingDifSite("NO");
				}
				newObs.setGeneralComments(generalComments.getText().toString());
				if(numofGraySquirrelsEditText.getText().toString().length()==0)
				{
					Toast.makeText(getApplicationContext(),"Please enter a value for Gray Squirrels",Toast.LENGTH_LONG).show();
					
				}
				else if(numofFoxSquirrelsEditText.getText().toString().length()==0)
				{
					Toast.makeText(getApplicationContext(),"Please enter a value for Fox Squirrels",Toast.LENGTH_LONG).show();
					
				}
				else
				{
				try
				{
					newObs.setNumOfFox("" + Integer.parseInt(numofFoxSquirrelsEditText.getText().toString()));
				}
				catch(NumberFormatException e)
				{
					Toast.makeText(getApplicationContext(),"Please enter a integer value for Grey Squirrels",Toast.LENGTH_LONG).show();
						newObs.setNumOfFox("0");
					//	finish();
				}
				try
				{
					newObs.setNumOfGray("" + Integer.parseInt(numofGraySquirrelsEditText.getText().toString()));
				}
				catch(NumberFormatException e)
				{
					Toast.makeText(getApplicationContext(),"Please enter a integer value for grey Squirrels",Toast.LENGTH_LONG).show();
					//finish();
					newObs.setNumOfGray("0");
				}

				Intent i = new Intent(NewObservation3.this, NewObservation4.class);
				//i.putExtra("OnEmulator", onEmulator);
		        startActivity(i);
			}
			}
	    });	    	
     }
     	@Override
	protected void onStart(){
		super.onStart();
		/*
 		Calendar calendar = new GregorianCalendar();
 	    int hour = calendar.get(Calendar.HOUR); 		
 	    if(hour>18 || hour<10)
 		{
 			LinearLayout l = (LinearLayout) findViewById(R.id.Screen);
 			int color=Color.WHITE;
 			l.setBackgroundColor(color);
 			
 		}
		*/
	}
	@Override
	protected void onDestroy() {
		//Location_Listener.stopListening();
		super.onDestroy();
	}
	
	@Override
	protected void onPause() {
		//Location_Listener.stopListening();
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		//startListening();
		super.onResume();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_MENU) ) {
	        
	    }
	    return super.onKeyDown(keyCode, event);
	}
	

	 private static boolean isAirplaneModeOn(Context context) {
	        return Settings.System.getInt(context.getContentResolver(),
	                Settings.System.AIRPLANE_MODE_ON, 0) != 0;

	 }   
	 
	 @Override
		public boolean onCreateOptionsMenu(Menu menu)
		{
		super.onCreateOptionsMenu(menu);

		menu.add(PREFERENCES_GROUP_ID, PAGE_2, 0, "Date/Time")
		.setIcon(R.drawable.menuicondatetime);
		menu.add(PREFERENCES_GROUP_ID, PAGE_3, 0, "General")
		.setIcon(R.drawable.menuicongencom);
		menu.add(PREFERENCES_GROUP_ID, PAGE_4, 0, "Setting")
		.setIcon(R.drawable.menuiconsetting);
		menu.add(PREFERENCES_GROUP_ID, PAGE_5, 0, "Trees")
		.setIcon(R.drawable.menuicontree);
		menu.add(PREFERENCES_GROUP_ID, PAGE_6, 0, "Food")
		.setIcon(R.drawable.menuiconfood);
		menu.add(PREFERENCES_GROUP_ID, PAGE_7, 0, "Predators")
		.setIcon(R.drawable.menuiconpred);

		return true;
		}
		
		@Override
		public boolean onOptionsItemSelected(MenuItem item)
		{
			Intent i;
			switch (item.getItemId()) {
			case PAGE_2:
				i = new Intent(NewObservation3.this, NewObservation2.class);
		        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
		        startActivity(i);
		        break;
			case PAGE_3:
				return true;
			case PAGE_4:
				i = new Intent(NewObservation3.this, NewObservation4.class);
		        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
		        startActivity(i);
		        break;
			case PAGE_5:
				i = new Intent(NewObservation3.this, NewObservation5.class);
		        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
		        startActivity(i);
		        break;
		    case PAGE_6:
				i = new Intent(NewObservation3.this, NewObservation6.class);
		        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
		        startActivity(i);
		        break;
			case PAGE_7:
				i = new Intent(NewObservation3.this, NewObservation7.class);
		        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
		        startActivity(i);
		        break;
			}
			return true;
		}
}
